import type { Ref } from 'vue';
import type { IMsgData, MsgType, IPostMsgData } from '@/api/leaveMsgApi';
import { reqLeaveMsg, sendLeaveMsg as sendLeaveMsgApi } from '@/api/leaveMsgApi';
import { watch } from 'vue';
import { useRequestBackHook } from '../common/requestHook';

const useLeaveMsg = (size: Ref<number>, page: Ref<number>) => {
  const {
    data,
    loading,
    refresh,
  } = useRequestBackHook(reqLeaveMsg(size.value, page.value));

  // 监听size 与 page 更改后触发刷新数据
  watch(
    () => [size, page],
    () => {
      refresh();
    }
  );
  const sendLeaveMsg = (postData: IPostMsgData) => {
    const { data, loading } = useRequestBackHook(sendLeaveMsgApi(postData));
    return {
      data,
      loading,
    };
  };
  return {
    data,
    loading,
    refresh,
    sendLeaveMsg,
  };
};

export default useLeaveMsg;
